﻿#region Using statements
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
#endregion

namespace RhythmComet
{
    public interface IPausedMenuState : IGameState { }

    public sealed class PausedMenuState : GameState, IPausedMenuState
    {
        Texture2D background;
        Vector2 backgroundPosition;
        SpriteFont font;
        Vector2 position;
#if !XBOX
        string[] menuOptions = new string[4] { "Continue", "Restart Song", "Back to Main Menu", "Quit" };
#else
        string[] menuOptions = new string[4] { "Continue", "Restart Game", "Back to Main Menu", "Quit" };
#endif
        int optionSelected = 0;

        public PausedMenuState(Game game)
            : base(game)
        {
            game.Services.AddService(typeof(IPausedMenuState), this);
        }

        public override void Update(GameTime gameTime)
        {
#if !XBOX
            if (Input.WasPressed(Keys.Space)
                && (GameManager.ContainsState(game.PlayingState.Value)))
#else
            if (Input.WasPressed(Buttons.Back) && GameManager.ContainsState(game.PlayingState.Value))
#endif
                GameManager.PopState();

#if !XBOX
            if (Input.WasPressed(Keys.Down) || Input.WasPressed(Buttons.DPadDown) || Input.WasPressed(Buttons.LeftThumbstickDown))
#else
            if (Input.WasPressed(Buttons.DPadDown) || Input.WasPressed(Buttons.LeftThumbstickDown))
#endif
                optionSelected = (optionSelected + 1) % 4;
#if !XBOX
            if (Input.WasPressed(Keys.Up) || Input.WasPressed(Buttons.DPadUp) || Input.WasPressed(Buttons.LeftThumbstickUp))
#else
                  if (Input.WasPressed(Buttons.DPadUp) || Input.WasPressed(Buttons.LeftThumbstickUp))
#endif
                optionSelected = optionSelected - 1;

            if (optionSelected < 0)
                optionSelected = 3;

#if !XBOX
            if (Input.WasPressed(Buttons.Start) || Input.WasPressed(Buttons.A) || Input.WasPressed(Keys.Enter))
#else
            if (Input.WasPressed(Buttons.Start) || Input.WasPressed(Buttons.A))
#endif
            {
                switch (optionSelected)
                {
                    case 0 :
                        GameManager.PopState();
                        break;
                    case 1 :
                        game.Comet.Reset();
                        game.BeatManager.Reset();
                        game.Score.Reset();
                        game.ObstacleManager.Reset();
#if !XBOX
                        game.Player.Stop();
                        game.Player.Initialize();
#endif
                        GameManager.PopState();
                        break;
                    case 2 :
                        game.Comet.Reset();
                        game.BeatManager.Reset();
                        game.Score.Reset();
                        game.ObstacleManager.Reset();
#if !XBOX
                        game.Player.Stop();
#endif
                        GameManager.ChangeState(game.StartMenuState.Value);
                        break;
                    case 3:
                        Game.Exit();
                        break;
                    default :
                        break;
                }
            }

            base.Update(gameTime);
        }

        public override void Draw(GameTime gameTime)
        {
            Color color;
            position = new Vector2(backgroundPosition.X + 40, 200);

            game.SpriteBatch.Begin();
            game.SpriteBatch.Draw(background, backgroundPosition, Color.White);

            for (int i = 0; i < menuOptions.Length; i++)
            {
                if (optionSelected == i)
                    color = Color.Red;
                else
                    color = Color.White;

                game.SpriteBatch.DrawString(font, menuOptions[i], position, color);
                position.Y += 20;
            }
            game.SpriteBatch.End();

            base.Draw(gameTime);
        }

        protected override void LoadContent()
        {
            font = game.Content.Load<SpriteFont>(@"fonts\GridExercise");
            background = game.Content.Load<Texture2D>("sprites/pauseBackground");
            backgroundPosition = new Vector2((game.GraphicsDevice.Viewport.Width / 2 - background.Width / 2),
                (game.GraphicsDevice.Viewport.Height / 2 - background.Height / 2));
        }
    }
}
